package cn.itcast.util;

import java.util.List;

import net.minidev.json.JSONObject;
import org.springframework.stereotype.Controller;


//import net.sf.json.JSONObject;

/**
 * 接口输出JSON形式
 * restful
 * {"code":"0|1","msg":message,"data":result,"count":总数}
 * code：0.成功；1.失败；；
 * msg：返回服务器信息
 * data：返回查询结果
 * count:返回数据总数
 * @author 欧阳辉
 *
 */
@Controller
public class OutputStringController {

    //400代表没有权限访问
    public String noAuth(String info){
        JSONObject result = new JSONObject();
        result.put("status", 400);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        result.put("data", "");
        return result.toString();
    }

    //911代表没有该账户已被冻结
    public String freezed(String info){
        JSONObject result = new JSONObject();
        result.put("status", 911);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        result.put("data", "");
        return result.toString();
    }
    //返回成功信息
    public String success(String info){
        JSONObject result = new JSONObject();
        result.put("status", 200);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        result.put("data", "");
        return result.toString();
    }
    //返回失败信息
    public String failure(String info){
        JSONObject result = new JSONObject();
        result.put("status",404);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        result.put("data", "");
        return result.toString();
    }
    //返回供前端使用的result-code成功
    public String resultSuccessCode(String info,Object resultStr,long count){
        JSONObject result = new JSONObject();
        result.put("code",0);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        if(resultStr == null)
            result.put("data", "");
        else
            result.put("data", resultStr);   
        result.put("count", count);
        return result.toString();
    }
  //返回供前端使用的result-Code失败
    public String resultFailureCode(String info,Object resultStr,long count){
        JSONObject result = new JSONObject();
        result.put("code",1);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        if(resultStr == null)
            result.put("data", "");
        else
            result.put("data", resultStr);
        result.put("count", count);
        return result.toString();
    }
    //返回供前端使用的result-成功
    public String resultSuccess(String info,Object resultStr,long count){
        JSONObject result = new JSONObject();
        result.put("status",200);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        if(resultStr == null)
            result.put("data", "");
        else
            result.put("data", resultStr);
        result.put("count", count);
        return result.toString();
    }
    //返回供前端使用的result-失败
    public String resultFailure(String info,Object resultStr,long count){
        JSONObject result = new JSONObject();
        result.put("status",404);
        if(info == null)
            result.put("msg", "");
        else
            result.put("msg", info);
        if(resultStr == null)
            result.put("data", "");
        else
            result.put("data", resultStr);
        result.put("count", count);
        return result.toString();
    }
    //返回分页前端数据
	
	/**
	 *@title: resultSuccess 
	 *@description: TODO
	 *@author: 欧阳辉
	 *@date: 2019年3月20日 下午5:12:34
	 *@param info
	 *@return
	 *@throws: 
	 */
    //无总数返回成功json数据
	public String resultSuccess(String info, Object resultStr) {
		 JSONObject result = new JSONObject();
	        result.put("status",200);
	        if(info == null)
	            result.put("msg", "");
	        else
	            result.put("msg", info);
	        if(resultStr == null)
	            result.put("data", "");
	        else
	            result.put("data", resultStr);
	        
	        return result.toString();
	}
	/**
	 *@title: resultSuccess 
	 *@description: TODO
	 *@author: 欧阳辉
	 *@date: 2019年3月20日 下午5:12:34
	 *@param info
	 *@return
	 *@throws: 
	 */
    //无总数返回成功json数据
	public String resultSuccessCode(String info, Object resultStr) {
		 JSONObject result = new JSONObject();
	        result.put("code",0);
	        if(info == null)
	            result.put("msg", "");
	        else
	            result.put("msg", info);
	        if(resultStr == null)
	            result.put("data", "");
	        else
	            result.put("data", resultStr);
	        
	        return result.toString();
	}
	
	/**
	 *@title: resultSuccess 
	 *@description: TODO
	 *@author: 欧阳辉
	 *@date: 2019年3月20日 下午5:12:34
	 *@param info
	 *@return
	 *@throws: 
	 */
    //无总数返回失败json数据
	public String resultFailure(String info, Object resultStr) {
		 JSONObject result = new JSONObject();
	        result.put("status",404);
	        if(info == null)
	            result.put("msg", "");
	        else
	            result.put("msg", info);
	        if(resultStr == null)
	            result.put("data", "");
	        else
	            result.put("data", resultStr);
	        
	        return result.toString();
	}
}